<template>
  <div class="addRent">
    <h3 class="title">添加租赁信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="租赁人" prop="rentUser">
        <el-input v-model="ruleForm.rentUser"></el-input>
      </el-form-item>
      <el-form-item label="租房地址" prop="rentInfo">
        <el-select
          v-model="ruleForm.rentInfo"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in houseList"
            :key="item.houseAddress"
            :label="item.houseAddress"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
       <!-- <el-form-item label="房东" prop="houseMaster">
        <el-input v-model="ruleForm.houseMaster"></el-input>
      </el-form-item> -->
      <el-form-item label="租金" prop="pay">
        <el-input v-model="ruleForm.pay"></el-input>
      </el-form-item>
      <el-form-item label="租赁时间" prop="rentStart">
        <el-date-picker
          v-model="ruleForm.rentStart"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到期时间" prop="rentEnd">
        <el-date-picker
          v-model="ruleForm.rentEnd"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >添加</el-button
        >
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import userRequest from "../request/user";
export default {
  props: ["clearComp"],
  data() {
    var rentUser = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写租赁人"));
      } else {
        return callback();
      }
    };
    var rentInfo = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写租赁信息"));
      } else {
        return callback();
      }
    };

    var pay = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写租金"));
      } else {
        return callback();
      }
    };
    var rentStart = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写租赁时间"));
      } else {
        return callback();
      }
    };
    var rentEnd = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写到期时间"));
      } else {
        return callback();
      }
    };
    /* var houseMaster = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写房东信息"));
      } else {
        return callback();
      }
    }; */

    return {
      houseList: [],
      userinfo: null,
      username: "",
      ruleForm: {
        rentUser: "",
        rentInfo: "",
        pay: "",
        rentStart: "",
        rentEnd: "",
        //houseMaster: "",
      },
      rules: {
        rentUser: [{ validator: rentUser, trigger: "blur" }],
        rentInfo: [{ validator: rentInfo, trigger: "blur" }],
        pay: [{ validator: pay, trigger: "blur" }],
        rentStart: [{ validator: rentStart, trigger: "blur" }],
        rentEnd: [{ validator: rentEnd, trigger: "blur" }],
        //houseMaster: [{ validator: houseMaster, trigger: "blur" }],
      },
    };
  },

  mounted() {
    this.getAllHouse();
  },

  methods: {
    close() {
      this.clearComp();
    },

    //
    getAllHouse() {
      (this.houseList = []),
        userRequest.getAllHouse().then((res) => {
          if (res.data.code === 200) {
            const result = res.data.result;
            this.houseList = result;
            console.log(this.houseList);
            setTimeout(() => {
              this.loading = false;
            }, 500);
          }
        });
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          userRequest
            .addRent({
              rentUser: this.ruleForm.rentUser,
              rentInfo: this.ruleForm.rentInfo,
              pay: this.ruleForm.pay,
              rentStart: this.ruleForm.rentStart,
              rentEnd: this.ruleForm.rentEnd,
              //houseMaster: this.ruleForm.houseMaster,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
                this.clearComp();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单数据
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
  },

  created() {
    // 获取本地用户信息
    this.userinfo = JSON.parse(window.localStorage.getItem("userinfo"));
    if (this.userinfo) {
      this.username = this.userinfo.userName;
    }
  },
};
</script>

<style lang="scss" scoped>
.addRent {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    display: flex;
    margin-top: 10px;
    height: 50px;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>